package com.ocom.mjrecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocom.common.entity.mjrecode.MjAttendance;
import com.ocom.common.request.access.AddAttendanceReq;
import com.ocom.common.utils.DateUtils;
import com.ocom.mjrecode.mapper.MjAttendanceMapper;
import com.ocom.mjrecode.service.MjAttendanceService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: ouqing
 * @Date: 2025/10/8
 **/
@Service
public class MjAttendanceServiceImpl implements MjAttendanceService {

    @Autowired
    private MjAttendanceMapper mapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void initInsert(MjAttendance mjAttendance) {
        mapper.insert(mjAttendance);
    }

    @Override
    public void initUpdateById(Long id, MjAttendance mjAttendance) {
        UpdateWrapper<MjAttendance> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", mjAttendance.getId());
        updateWrapper.eq("com_id", mjAttendance.getComId());
        updateWrapper.eq("month_value", mjAttendance.getMonthValue());
        mapper.update(mjAttendance, updateWrapper);
    }

    //  @Transactional
    @Override
    public void initBatchInsert(List<MjAttendance> mjAttendanceList) {
        mjAttendanceList.forEach(mj -> {
                    mapper.insert(mj);
                }
        );
    }

    //  @Transactional
    @Override
    public void initBatchUpdate(List<MjAttendance> list) {
        list.forEach(mj -> {
            UpdateWrapper<MjAttendance> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", mj.getId());
            updateWrapper.eq("com_id", mj.getComId());
            updateWrapper.eq("month_value", mj.getMonthValue());
            mapper.update(mj, updateWrapper);
        });
    }


//    @Override
//    public MjAttendance existData(AddAttendanceReq req) {
//        String monthValue = DateUtils.getStrDate(new Date(), "yyyyMM");
//        QueryWrapper<MjAttendance> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("com_id", req.getComId());
//        queryWrapper.eq("p_id", req.getPId());
//        queryWrapper.eq("role_id", req.getId());
//        queryWrapper.eq("month_value", monthValue);
//        queryWrapper.eq("attendance_date", DateUtils.getDate());
//        return mapper.selectOne(queryWrapper);
//    }



    @Override
    public void addOutdoorData(AddAttendanceReq req) {
        if (req.getTimeType() != 2) {
            return;
        }
        String monthValue = DateUtils.getStrDate(new Date(), "yyyyMM");

        //检查数据是否存在  存在数据则不处理

        QueryWrapper<MjAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", req.getComId());
        queryWrapper.eq("p_id", req.getPId());
        queryWrapper.eq("role_id", req.getId());
        queryWrapper.eq("attendance_type", req.getAttendanceType());
        queryWrapper.eq("month_value", monthValue);
        queryWrapper.eq("attendance_date", DateUtils.getDate());
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        MjAttendance mjAttendance =  mapper.selectOne(queryWrapper);

        if(mjAttendance!=null){
            return;
        }

        //查上一条进的数据
        QueryWrapper<MjAttendance> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("com_id", req.getComId());
        queryWrapper1.eq("p_id", req.getPId());
        queryWrapper1.eq("month_value", monthValue);
        queryWrapper1.eq("time_type", 1 );
        queryWrapper1.eq("attendance_type", req.getAttendanceType());
        queryWrapper1.eq("attendance_date", DateUtils.getDate());
        queryWrapper1.orderByDesc("id");
        queryWrapper1.last("limit 1");
        MjAttendance mjAttendance2 =  mapper.selectOne(queryWrapper1);

        if(mjAttendance2==null){
            return;
        }

        mjAttendance2.setId(null);
        mjAttendance2.setRoleId(req.getId());
        mjAttendance2.setAttendanceType(req.getAttendanceType());
        mjAttendance2.setTimeType(req.getTimeType());
        mjAttendance2.setRoleName(req.getRuleName());
        mjAttendance2.setAttendanceDateTime(null);
        if(req.getAttendanceType()==1){
            mjAttendance2.setExpectPoint(3);
        }else{
            mjAttendance2.setExpectPoint(1);
        }
        if(mjAttendance2.getAttendanceResult()!=1){
            if(mjAttendance2.getAttendanceResult()==0){
                mjAttendance2.setAttendanceResult(3);
                mjAttendance2.setAttendanceResultName("缺卡");
            }
            if(mjAttendance2.getAttendanceResult()==3){
                mjAttendance2.setAttendanceResult(1);
                mjAttendance2.setAttendanceResultName("正常");
            }
        }

        initInsert(mjAttendance2);

    }
}
